package com.example.cofeed.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.blankj.utilcode.util.ObjectUtils;
import com.example.cofeed.R;
import com.example.cofeed.constant.IntentKeyConstant;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by mqwang on 2017/11/20.
 */
public class ShareActivity extends Activity {

    private UMWeb web;
    private String url;
    private String content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share);
        ButterKnife.bind(this);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        String smallimgs = getIntent().getStringExtra(IntentKeyConstant.SMALLIMGS);
        UMImage localImage;
        if (TextUtils.isEmpty(smallimgs)){
            localImage = new UMImage(this, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

        }else {
            localImage = new UMImage(this, smallimgs);

        }

        url = getIntent().getStringExtra(IntentKeyConstant.URL);

        content = getIntent().getStringExtra(IntentKeyConstant.NAME);
        String title = getIntent().getStringExtra(IntentKeyConstant.TITLE);
        if (ObjectUtils.isNotEmpty(url)) {
            url = url.replace("//app", "/m");
            web = new UMWeb(url);//网址
        } else {
            web = new UMWeb("http://m.cofeed.com/cofapp/cofsms/");//网址
        }


        if (ObjectUtils.isNotEmpty(title)){
            web.setTitle(title);//标题
        }else {
            web.setTitle("天下粮仓网");//标题
        }
        web.setThumb(localImage);  //缩略图
        if (ObjectUtils.isEmpty(content)) {
            content = " ";
//            content = "天下粮仓是由天下粮仓网旗下APP，专业粮油饮料行业打造的短讯客户端产品，集合短信服务与手机客户端的优点,是一款可以根据您自身需求量身定制信息推送服务应用软件！操作简单明了,接收信息内容更加及时方便快捷。万千资讯，尽在“掌”握，助您抢占市场先机!";
//            content = "天下粮仓是由天下粮仓网旗下APP，专业粮油饮料行业打造的短讯客户端产品，集合短信服务与手机客户端的优点,是一款可以根据您自身需求量身定制信息推送服务应用软件！操作简单明了,接收信息内容更加及时方便快捷。万千资讯，尽在“掌”握，助您抢占市场先机!";
        }
        web.setDescription(content);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.weixin_iv, R.id.friends_iv, R.id.close_iv, R.id.sina_iv, R.id.qq_iv, R.id.zone_iv, R.id.sms_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.weixin_iv:
                new ShareAction(this).withMedia(web)
                        .setPlatform(SHARE_MEDIA.WEIXIN)
                        .setCallback(shareListener).share();
                break;
            case R.id.friends_iv:
                new ShareAction(this).withMedia(web)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(shareListener).share();
                break;
            case R.id.sina_iv:
                new ShareAction(this).withMedia(web)
                        .setPlatform(SHARE_MEDIA.SINA)
                        .setCallback(shareListener).share();
                break;
            case R.id.qq_iv:
                new ShareAction(this).withMedia(web)
                        .setPlatform(SHARE_MEDIA.QQ)
                        .setCallback(shareListener).share();
                break;
            case R.id.zone_iv:
                new ShareAction(this).withMedia(web)
                        .setPlatform(SHARE_MEDIA.QZONE)
                        .setCallback(shareListener).share();
                break;
            case R.id.sms_iv:

//                String smsBody = content + "链接地址：" + url;
                new ShareAction(this).withMedia(web)
                        .setPlatform(SHARE_MEDIA.SMS)
                        .setCallback(shareListener).share();
//                Uri smsToUri = Uri.parse("smsto:");
//                Intent mIntent = new Intent(Intent.ACTION_SENDTO, smsToUri);
//                mIntent.putExtra("sms_body", smsBody);
//                startActivity(mIntent);
                break;
            case R.id.close_iv:
//                new ShareAction(this).withMedia(web).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QZONE, SHARE_MEDIA.SMS)
//                        .setCallback(shareListener).open();

                break;

        }
        finish();

    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(ShareActivity.this, "成功了", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ShareActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ShareActivity.this, "取消了", Toast.LENGTH_LONG).show();

        }
    };

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
